package com.younion.action.consumer.impl;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.younion.action.consumer.AbstractRequestConsumer;
import com.younion.action.interactive.mobile.ApiConstant;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.business.ICardEntityService;
import com.younion.business.IFavoritesService;
import com.younion.business.IFavourableEntityService;
import com.younion.model.FavoritesOperateResult;
import com.younion.util.HttpRequestUtils;
import com.younion.util.JsonUtils;

public class FavoritesCancelConsumer extends AbstractRequestConsumer {
	private IFavoritesService favoritesService;
	private ICardEntityService cardEntityService;
	private IFavourableEntityService favourableEntityService;

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return cancleFavorites(request, response);
	}

	public String cancleFavorites(HttpServletRequest request,
			HttpServletResponse response) {
		String user_token = request.getParameter("user_token");
		String favourId = request.getParameter("favour_id");
		String type = request.getParameter("type");
		FavoritesOperateResult result = new FavoritesOperateResult(); 
		ResultObject resultObject = new ResultObject();
		try {
			Map paramMap = new HashMap();
			paramMap.put("userToken", user_token);
			paramMap.put("favourId", favourId);
			paramMap.put("type", type);
			favoritesService.removeByMap(paramMap);
			Map updateFavourAmountMap = new HashMap();
			updateFavourAmountMap.put("id", favourId);
			updateFavourAmountMap.put("amount", -1);
			if ("1".equals(type)) {
				cardEntityService
						.updateFavourAmountByMap(updateFavourAmountMap);
			} else if ("2".equals(type)) {
				favourableEntityService
						.updateFavourAmountByMap(updateFavourAmountMap);
			}
			resultObject.setStatus(ApiConstant.SUCCESS_STATUS);
			result.setSuccessful("1");
			resultObject.setResult(result);
			resultObject.setMsg(ApiConstant.FAVORITES_CANCLE_SUCCESS);
		} catch (Exception e) {
			resultObject.setStatus(ApiConstant.FALSE_STATUS);
			result.setSuccessful("0");
			resultObject.setResult(result);
			resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
			e.printStackTrace();
		}
		HttpRequestUtils.printContent(JsonUtils.parseObj2Json(resultObject),
				response);
		return null;
	}

	public void setFavoritesService(IFavoritesService favoritesService) {
		this.favoritesService = favoritesService;
	}

	public void setCardEntityService(ICardEntityService cardEntityService) {
		this.cardEntityService = cardEntityService;
	}

	public void setFavourableEntityService(
			IFavourableEntityService favourableEntityService) {
		this.favourableEntityService = favourableEntityService;
	}

}
